package com.qf.com.lantu.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.com.lantu.common.vo.Result;
import com.qf.com.lantu.sys.entity.User;
import com.qf.com.lantu.sys.service.IUserService;

import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zqh
 * @since 2025-03-26
 */
//跨域
//@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private IUserService userServic;
    @GetMapping("/all")
    public Result<List<User>> getAll(){
        List<User> list = userServic.list();
        return Result.success(list,"出来le");
    }

    //登录
    @PostMapping("/login")
    //@RequestBody 将json数据封装到对象中
    public Result<Map<String ,Object>> login(@RequestBody User user){
        Map<String,Object> data = userServic.login(user);
        if (data != null){
            return Result.success(data);
        }
       return Result.fail(200002,"用户名或密码错误");
    }

    @GetMapping("/info")
    public Result<Map<String,Object>> getUserInfo(@RequestParam("token") String token){
        //根据touken获取用户信息
        Map<String ,Object> data = userServic.getUserInfo(token);
        if (data != null) {
            return Result.success(data);
        }

        return Result.fail(20003,"登录信息无效，请重新登录");
    }

    //注销
    @PostMapping("/logout")
    public Result<?> logout(@RequestHeader("X-Token") String token){
        userServic.logout(token);
        return Result.success();
    }

    //查询
    @GetMapping("/list")
    public Result<Map<String, Object>> getUserList(
            //传给前端展示表格的数据
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "pageNo") Long pageNo,
            @RequestParam(value = "pageSize") Long pageSize) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(username), User::getUsername, username);
        wrapper.like(StringUtils.hasLength(phone), User::getPhone, phone); // 改为like模糊查询
        //按照id降序
//        wrapper.orderByDesc(User::getId);
        Page<User> page = new Page<>(pageNo, pageSize);
        userServic.page(page, wrapper);

        Map<String, Object> data = new HashMap<>();
        data.put("total", page.getTotal());
        data.put("rows", page.getRecords());

        return Result.success(data);
    }
    //新增
    @PostMapping
    //Result<?> 返回空数据就行，@RequestBody转化传给前端的数据
    public Result<?> addUser(@RequestBody User user){
        userServic.save(user);
        return Result.success("新增用户成功");
    }

    //修改
    @PutMapping
    //Result<?> 返回空数据就行，@RequestBody转化传给前端的数据
    public Result<?> updateUser(@RequestBody User user){
        user.setPassword(null);
        userServic.updateById(user);
        return Result.success("修改用户成功");
    }

    //根据id查询
    @GetMapping("/{id}")
    public Result<User> getUserById(@PathVariable("id") Integer id){
        User byId = userServic.getById(id);
        return Result.success(byId);

    }
    //删除
    @DeleteMapping("/{id}")
    public Result<?> deleteUser(@PathVariable Long id) {
        userServic.removeById(id);
        return Result.success("删除用户成功");
    }


    }





